import { http } from "@/utils/http";
import { baseAdminURLApi } from "../utils";

export interface OperationLogDTO {
  businessType?: number;
  businessTypeStr?: string;
  calledMethod?: string;
  errorStack?: string;
  id?: number;
  operationParam?: string;
  operationResult?: string;
  operationTime?: string;
  operatorIp?: string;
  operatorLocation?: string;
  operatorType?: number;
  operatorTypeStr?: string;
  requestMethod?: string;
  requestModule?: string;
  requestUrl?: string;
  status?: number;
  userId?: number;
  username?: string;
  requestDesc: string;
}
/** 通用基础查询字段 */
export interface BaseQuery {
  beginTime?: string;
  endTime?: string;
  status?: string;
  username?: string;
}

/** 操作日志查询 */
export interface OperationQuery extends BaseQuery {
  module?: string;
  operationTime?: string;
}

/** 登录日志查询 */
export interface LoginLogQuery extends BaseQuery {
  ipAddress?: string;
}

/** 获取操作日志列表 */
export const getOperationLogsList = (params?: OperationQuery) => {
  return http.request<Array<OperationLogDTO>>(
    "get",
    baseAdminURLApi("/logs/operation"),
    {
      params
    }
  );
};

export const deleteOperationLog = (operationIds: Array<number>) => {
  return http.request<boolean>("delete", baseAdminURLApi("/logs/operation"), {
    data: operationIds
  });
};
// 删除所有操作日志
export const deleteAllOperationLog = () => {
  return http.request<boolean>(
    "delete",
    baseAdminURLApi("/logs/operation/all")
  );
};
// 导出操作日志 将符合条件的操作日志全部导出到excel
export const exportOperationLogExcel = (
  fileName: string,
  data: OperationQuery
) => {
  http.download(baseAdminURLApi("/logs/operation/export"), fileName, "导出", {
    params: { fileName }, // 作为 URL 参数
    data // 作为 JSON 请求体
  });
};

/**
 * 登录日志信息
 */
export interface LoginLogsDTO {
  browser?: string;
  infoId?: string;
  ipAddress?: string;
  loginLocation?: string;
  loginTime?: Date;
  msg?: string;
  operationSystem?: string;
  /** TODO 这个登录状态的设计很奇怪  需要重构掉 */
  status?: number;
  statusStr?: string;
  username?: string;
}

/** 获取登录日志列表 */
export const getLoginLogsList = (params?: LoginLogQuery) => {
  return http.request("get", baseAdminURLApi("/logs/login"), {
    params
  });
};

// 导出用户信息 Excel
export const exportLoginLogExcel = (fileName: string, data: OperationQuery) => {
  http.download(baseAdminURLApi("/logs/login/export"), fileName, "导出", {
    params: { fileName }, // 作为 URL 参数
    data // 作为 JSON 请求体
  });
};

export const deleteLoginLog = (data: Array<number>) => {
  return http.request<boolean>("delete", baseAdminURLApi("/logs/login"), {
    data
  });
};
export const deleteAllLoginLog = () => {
  return http.request<boolean>("delete", baseAdminURLApi("/logs/login/all"));
};
